<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="新闻标题" prop="newTitle">
        <el-input
          v-model="queryParams.newTitle"
          placeholder="请输入新闻标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻类型" prop="newType">
        <el-select v-model="queryParams.newType" placeholder="请选择新闻类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table  border v-loading="loading" :data="newsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="新闻ID" align="center" prop="newId" />
      <el-table-column label="新闻标题" align="center" prop="newTitle" />
      <el-table-column label="新闻类型" align="center" prop="newType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_notice_type" :value="scope.row.newType"/>
        </template>
      </el-table-column>
      <el-table-column label="新闻内容" align="center" prop="newContent" >
        <template slot-scope="scope">
          <el-button type="primary" @click="view(scope.row.newContent)">查 看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime"  width="180px"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180px" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="审核状态" align="center" prop="auditStatus" :formatter="formAuditStatus"/>
      <el-table-column label="拒绝原因" align="center" prop="refuseReason" >
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === 2">
            {{scope.row.refuseReason}}
         </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:news:edit']"
          >编辑/查看</el-button>
          <el-button
            v-show="scope.row.auditStatus !=1 "
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="aduit(scope.row)"
            v-hasPermi="['system:news:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:news:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--修改新闻发布管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新闻标题" prop="newTitle">
          <el-input :disabled="form.auditStatus == 1 ? true: false" v-model="form.newTitle" placeholder="请输入新闻标题" style="width: 360px" show-word-limit maxlength="50"/>
        </el-form-item>
        <el-form-item label="新闻类型" prop="newType">
          <el-select :disabled="form.auditStatus == 1 ? true: false" v-model="form.newType" placeholder="请选择新闻类型">
            <el-option
              v-for="dict in dict.type.sys_notice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新闻封面" prop="newsImage">
          <image-upload :disabled="form.auditStatus == 1 ? true: false" v-model="form.newsImage"/>
        </el-form-item>
        <el-form-item label="新闻内容">
          <editor editor v-model="form.newContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :disabled="form.auditStatus == 1 ? true : false" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary":disabled="form.auditStatus == 1 ? true: false" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--审核状态-->
    <el-dialog :title="aduitTitle" :visible.sync="openStauts" width="650px" append-to-body>
      <el-form ref="formAduit" :model="formAduit" :rules="rulesAduit" label-width="80px">
        <el-form-item :label="'审核结果'" prop="auditStatus">
          <el-radio-group v-model="formAduit.auditStatus" name="audit_form_status">
            <el-radio :label="1">{{'审核通过' }}</el-radio>
            <el-radio :label="2">{{'审核不通过'}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="formAduit.auditStatus == 2" :label="'审核拒绝原因'" prop="refuseReason" >
          <el-input v-model="formAduit.refuseReason" name="audit_form_refusalCause" :rows="4" type="textarea" maxlength="120" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAduit">提交审核</el-button>
        <el-button @click="cancelAduit">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="divisible" width="780px" append-to-body>
    <el-card>
      <div v-html="content"></div>
    </el-card>
    </el-dialog>
  </div>
</template>

<script>
import {listNews, getNews, delNews, addNews, updateNews, auditNews} from "@/api/system/news";

export default {
  name: "News",
  dicts: ['sys_notice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻发布管理表格数据
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      aduitTitle: '审核',
      openStauts: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newTitle: null,
        newType: null,
        newContent: null,
        status: null,
      },
      // 表单参数
      form: {
         auditStatus:null
      },
      formAduit:{},
      // 表单校验
      rules: {
        newTitle: [
          { required: true, message: "新闻标题不能为空", trigger: "blur" }
        ],
        newType: [
          { required: true, message: "新闻类型不能为空", trigger: "change" }
        ],
        newsImage: [
          { required: true, message: "新闻封面不能为空", trigger: "change" }
        ],
      },
      rulesAduit: {
        auditStatus: [{required:true, message:"审核状态不能为空",trigger: "blur"}],
        refuseReason: [
          { required: true, message: "拒绝理由不能为空", trigger: "change" }
        ],
      },
      content:'',
      divisible:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    view(newContent){
      this.content = newContent
      this.divisible = true
    },
    formAuditStatus(row){
     switch (row.auditStatus) {
       case 0:
         return '待审核';
         break;
       case 1:
         return '审核通过';
         break;
       case 2:
         return '审核拒绝';
         break;
       default:
           break;
     }
    },
    /** 查询新闻发布管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.tag = 1
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelAduit() {
      this.openStauts = false;
      this.resetAduit();
    },
    // 表单重置
    reset() {
      this.form = {
        newId: null,
        newTitle: null,
        newType: null,
        newContent: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    resetAduit() {
      this.formAduit= {
        auditStatus:null,
        refuseReason:null
      };
      this.resetForm("formAduit");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.newId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加新闻发布管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const newId = row.newId || this.ids
      getNews(newId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改新闻发布管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.newId != null) {
            updateNews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**
     * 提交审核
     */
    submitFormAduit(){
      const self = this
      self.$refs["formAduit"].validate(valid => {
        if (valid) {
          if (self.formAduit.newId != null) {
            debugger
            auditNews(self.formAduit).then(response => {
              self.$modal.msgSuccess("提交成功");
              self.openStauts = false;
              self.getList();
            });
          }
        }
      });
    },
    /**
     * 审核
     */
    aduit(row){
      const self = this
      self.formAduit.newId = row.newId
      self.openStauts = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const newIds = row.newId || this.ids;
      this.$modal.confirm('是否确认删除新闻发布管理编号为"' + newIds + '"的数据项？').then(function() {
        return delNews(newIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/news/export', {
        ...this.queryParams
      }, `news_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
